package com.controller;

import com.annotation.Encrypted;
import com.formatter.CustomizedDateTimeFormatter;
import com.methodargumentresolver.UserMethodArgumentResolver;
import com.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@RestController
public class WelcomeController {


    public static final String customizedDateTimeFormat = "yyyy|MM|dd-->HH:mm:ss";

    @InitBinder
    public void formatDateTime(WebDataBinder webDataBinder){
        CustomizedDateTimeFormatter formatter = new CustomizedDateTimeFormatter(DateTimeFormatter.ofPattern(customizedDateTimeFormat));
        webDataBinder.addCustomFormatter(formatter);
    }

    @GetMapping("/he")
    public String updateDate(LocalDateTime dateTime) {

        return "hello->"+DateTimeFormatter.ISO_TIME.format(dateTime);
    }

    /**
     * 因为 {@link RequestResponseBodyMethodProcessor} 解析器的优先级
     * 比 {@link UserMethodArgumentResolver} 高
     * 所以这里只会应用 {@link RequestResponseBodyMethodProcessor} 解析器
     * @param userVo
     * @return
     */
    @PostMapping("/updateUserInfo")
    public String updateUserInfo( @RequestBody @Encrypted UserVo userVo){
        return userVo.getName()+"解密成功啦";
    }

}
